package
{
	import com.anttikupila.revolt.Revolt;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.ui.Mouse;
	
	import models.vo.UserVO;
	
	import mx.events.Request;
	
	import org.dexter.DexterFramework;
	import org.dexter.sendDexterEvent;
	
	import views.P2PVideo;

	public class MainUI extends VideoBackground
	{
		private var _background:MovieClip;
		private var revolt:Revolt = new Revolt(320, 240);
		private var loader:Loader = new Loader();
		public var p2pVideo:P2PVideo = new P2PVideo();
		public function MainUI()
		{
			super();
			cacheAsBitmap = true;
			publishLight.gotoAndStop(1);
			stopBn.visible = false;
			p2pVideo.x = videoBack.x;
			p2pVideo.y = videoBack.y;
			fullBn.addEventListener(MouseEvent.CLICK,onDoubleClick);
			stopBn.addEventListener(MouseEvent.CLICK,onStopPublish);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(event:Event):void{
				_background = loader.content as MovieClip;
				videoBack.addChild(_background);
			});
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,function(event:Event):void{
				sendDexterEvent("callJs",'jTrace',"加载背景失败");
			});
			addChild(p2pVideo);
		}
		public function initUI():void{
			stage.doubleClickEnabled = true;
			stage.addEventListener(MouseEvent.DOUBLE_CLICK,onDoubleClick);
		}
		public function setUser(user:UserVO):void{
			p2pVideo.setUser(user);
			if(user){
				videoName.text = user.name;
				if(UserVO.self.role == UserVO.ADMIN){
					stopBn.visible = true;
				}
				stopEffect();
				sendDexterEvent("callJs",'notification',{msg:videoName.text+"的视频已经停靠",showType:'slide'});
			}else{
				stopBn.visible = false;
				sendDexterEvent("callJs",'notification',{msg:videoName.text+"的视频已经停止发布",showType:'slide'});
				videoName.text = "";
				startEffect();
			}
		}
		private function onDoubleClick(event:MouseEvent):void{
			if(stage.displayState == StageDisplayState.FULL_SCREEN){
				stage.displayState = StageDisplayState.NORMAL;
			}else{
				stage.displayState = StageDisplayState.FULL_SCREEN;
			}
		}
		private function onStopPublish(event:MouseEvent):void{
			sendDexterEvent("stopPublish");
		}
		[DexterExternal][DexterEvent]
		public function setBackground(v:String):void{
			if(v)
			try{
				loader.load(new URLRequest(v),new LoaderContext(false,ApplicationDomain.currentDomain));
			}catch(e:Error){
				sendDexterEvent("callJs","jsTrace",e.message);
			}
		}
		[DexterExternal][DexterEvent]
		public function playMusic(mp3:Object):void{
			stopMusic();
			sendDexterEvent("callJs","jsTrace","music:"+mp3.name);
			videoBack.addChild(revolt);
			revolt.playMP3(mp3.url);
			if(_background && videoBack.contains(_background)){
				_background.stopEffect();
				videoBack.removeChild(_background);
			}
		}
		[DexterExternal][DexterEvent]
		public function stopMusic():void{
			if(revolt.soundChannel){
				revolt.stopMP3();
				videoBack.removeChild(revolt);
				videoBack.addChild(_background);
				if(publishLight.currentFrame == 1)_background.startEffect();
			}
		}
		public function startEffect():void{
			publishLight.gotoAndStop(1);
			try{
				if(!videoBack.contains(revolt)){
					_background.startEffect();
					videoBack.addChild(_background);
				}
			}catch(e:Error){
				
			}
		}
		public function stopEffect():void{
			publishLight.gotoAndStop(2);
			try{
				if(videoBack.contains(_background)){
					_background.stopEffect();
					videoBack.removeChild(_background);
				}
			}catch(e:Error){
				
			}
		}
	}
}